﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using MvcEngine.Core;
using MvcEngine.Core.Services;
using MvcEngine.Mvc.Extensions;
using MvcEngine.Mvc.Constraints;
using MvcEngine.Core.HttpHandlers;
using MvcEngine.Core.Content;
using System.Web.Routing;

namespace MvcEngine.Mvc.Areas.BackOffice
{
    public class BackOfficeRoutes : AreaRegistration
    {
        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapMvcEngineRoute(
              "BackOffice_Default",
              "BackOffice",
              new { controller = "Home", action = "Index" },
              new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            RegisterModuleRoutes(context);
            RegisterPageRoutes(context);
            RegisterPageContentRoutes(context);
            RegisterAdminModuleRoutes(context);

            RegisterSiteRoutes(context);
            RegisterLogRoutes(context);
            RegisterUserRoutes(context);

            //context.MapMvcEngineRoute(
            //     "BackOffice_ModuleCssFile",
            //     "BackOffice/Module/{moduleName}/Css/{fileName}",
            //     new string[] { "MvcEngine.Mvc.BackOffice.Controllers" },
            //     new CssHandler()
            //);

            context.MapMvcEngineRoute(
              "BackOffice_ControlCommand",
              "BackOffice/PageContent/{ctrId}/Command/{name}",
              new { controller = "Home", action = "Command" },
              new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
              "BackOffice_RestartApplication",
              "BackOffice/RestartApplication",
              new { controller = "Home", action = "RestartApplication" },
              new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
              "BackOffice_DefaultAll",
              "BackOffice/{*all}",
              new { controller = "Error", action = "Error", viewName = "PageNotFound" },
              new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );
        }

        protected virtual void RegisterModuleRoutes(AreaRegistrationContext context)
        {
            var modules = ContentManager.Current.GetModules();
            foreach (var item in modules)
            {
                var controls = ContentManager.Current.GetControls(item.Id);
                foreach (var control in controls)
                {
                    if (!string.IsNullOrEmpty(control.ControlServiceType))
                    {
                        Type type = Type.GetType(control.ControlServiceType);
                        IControlService service = (IControlService)IoC.Resolve(type);
                        var controlRoutes = new RouteCollection();
                        service.RegisterBackofficeRoutes(new AreaRegistrationContext("BackOffice", controlRoutes, null));
                        foreach (var routeBase in controlRoutes)
                        {
                            var route = routeBase as Route;
                            if (route != null)
                            {
                                route.Defaults.Add("controlKey", control.ControlKey);
                                context.Routes.Add((string)route.DataTokens["RouteName"], route);
                            }
                        }
                    }
                }
            }
        }

        protected virtual void RegisterPageRoutes(AreaRegistrationContext context)
        {
            context.MapMvcEngineRoute(
                "BackOffice_Pages",
                "BackOffice/Pages",
                new { controller = "Page", action = "List" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_NewPage",
                "BackOffice/Pages/New",
                new { controller = "Page", action = "New" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_DeletePage",
                "BackOffice/Pages/Delete/{pageId}",
                new { controller = "Page", action = "Delete" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_EditPage",
                "BackOffice/Pages/Edit/{pageId}",
                new { controller = "Page", action = "Edit" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_PageMoveUp",
                "BackOffice/Page/MoveUp/{pageId}",
                new { controller = "Page", action = "MoveUp" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_PageMoveDown",
                "BackOffice/Page/MoveDown/{pageId}",
                new { controller = "Page", action = "MoveDown" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );
        }

        protected virtual void RegisterPageContentRoutes(AreaRegistrationContext context)
        {
            context.MapMvcEngineRoute(
                "BackOffice_RenamePageContent",
                "BackOffice/Page/Content/Rename/{id}",
                new { controller = "PageContent", action = "Rename", id = "" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );


            context.MapMvcEngineRoute(
                "BackOffice_AddModuleToPage",
                "BackOffice/Page/{pageId}/{commandName}/Module/{moduleId}",
                new { controller = "PageContent", action = "InstallModule", moduleId = "" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_PageContentDelete",
                "BackOffice/Page/Content/Delete/{id}",
                new { controller = "PageContent", action = "Delete" },
                new { id = new IntegerConstraint() },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_PermissionsPageContent",
                "BackOffice/Page/Content/Permissions/{id}",
                new { controller = "PageContent", action = "Permissions" },
                new { id = new IntegerConstraint() },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_PageContentSave",
                "BackOffice/Page/Content/Save/{pageId}",
                new { controller = "PageContent", action = "Save" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_AddControlToPage",
                "BackOffice/Page/{pageId}/Control/Add",
                new { controller = "PageContent", action = "Add" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_PageTemplate",
                "BackOffice/Page/{mode}/Template/{pageId}",
                new { controller = "PageContent", action = "Design" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );
        }

        protected virtual void RegisterLogRoutes(AreaRegistrationContext context)
        {
            context.MapMvcEngineRoute(
                "BackOffice_Logs",
                "BackOffice/Logs",
                new { controller = "Log", action = "List" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_LogsPaging",
                "BackOffice/Logs/Page{pageNumber}",
                new { controller = "Log", action = "List" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_DeleteLog",
                "BackOffice/Logs/Delete/{id}",
                new { controller = "Log", action = "Delete" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_LogDetails",
                "BackOffice/Logs/Details/{id}",
                new { controller = "Log", action = "Details" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_ClearLogs",
                "BackOffice/Logs/Clear",
                new { controller = "Log", action = "Clear" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );
        }

        protected virtual void RegisterSiteRoutes(AreaRegistrationContext context)
        {
            context.MapMvcEngineRoute(
                "BackOffice_Sites",
                "BackOffice/Sites",
                new { controller = "Site", action = "Index" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_EditSite",
                "BackOffice/Site/Edit/{siteId}",
                new { controller = "Site", action = "Edit" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_RefreshCache",
                "BackOffice/Site/RefreshCache/{siteId}",
                new { controller = "Site", action = "RefreshCache" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );
        }

        protected virtual void RegisterAdminModuleRoutes(AreaRegistrationContext context)
        {
            context.MapMvcEngineRoute(
                "BackOffice_Modules",
                "BackOffice/Modules",
                new { controller = "Module", action = "List" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_NewModule",
                "BackOffice/Modules/New",
                new { controller = "Module", action = "New" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_InstallModule",
                "BackOffice/Modules/Install",
                new { controller = "Module", action = "Install" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_UninstallModule",
                "BackOffice/Modules/UnInstall/{id}",
                new { controller = "Module", action = "UnInstall" },
                new { id = new IntegerConstraint() },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );
        }

        protected virtual void RegisterUserRoutes(AreaRegistrationContext context)
        {
            context.MapMvcEngineRoute(
                "BackOffice_Users",
                "BackOffice/Users",
                new { controller = "User", action = "List" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_UsersPaging",
                "BackOffice/Users/Page{pageNumber}",
                new { controller = "User", action = "List" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_EditUser",
                "BackOffice/User/Edit/{id}",
                new { controller = "User", action = "Edit" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_DeleteUser",
                "BackOffice/User/Delete/{id}",
                new { controller = "User", action = "Delete" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_LogOff",
                "BackOffice/LogOff",
                new { controller = "User", action = "LogOff" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_LogOn",
                "BackOffice/LogOn",
                new { controller = "User", action = "LogOn" },
                new string[] { "MvcEngine.Mvc.BackOffice.Controllers" }
            );
        }

        public override string AreaName
        {
            get { return "BackOffice"; }
        }
    }
}
